package com.hymanting.blog.resource;

import com.hymanting.blog.application.ArticleApplicationService;
import com.hymanting.blog.domain.article.Article;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author hxchen
 * @Date 2021/6/20
 */
@Path("/articles")
@Component
//@CacheConfig(cacheNames = "resource.account")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class ArticleResource {

    @Inject
    private ArticleApplicationService service;

    /**
     * 根据文章Id获取文章详情
     */
    @GET
    @Path("/{id}")
//    @Cacheable(key = "#articleid")
    public Article getArticle(@PathParam("id") Integer articleId) {
        Article article = service.findArticleByArticleId(articleId);
        return article;
    }

    @POST
    @Path("/create")
//    @Cacheable(key = "#articleid")
    //@RequestBody Map articleMap
    public boolean createArticle(@RequestBody Article article) {
        article.setAuthorId(1);
//        article.setStatus(Integer.valueOf(articleMap.get("status").toString()));
//        article.setTitle(articleMap.get("title").toString());
//        article.setContent(articleMap.get("content").toString());
        article.setTopic("tech");
        article.setCreateTime(new Date());
        return service.createArticle(article);
    }

    public List<Article> getAuthorPublishedArticlePagedList(@QueryParam("authorId") String authorId) {
        return service.findPagedArticlesByAuthorId(authorId);
    }

    @GET
    @Path("/all")
    public List<Article> getAdminArticles() {
        return service.getPagedArticles(null);
    }

    @GET
    @Path("/")
    public List<Article> getRecentPublishedArticlePagedList() {
        return service.getPagedArticles(1);
    }
}
